package com.shujia;

import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class Code6PrepareStatement02 {
    static Connection connection;
    static {
        Properties properties = new Properties();
        try {
            properties.load(new FileReader("source/jdbc.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Class.forName(properties.getProperty("driver"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            connection = DriverManager.getConnection(
                    properties.getProperty("url"),
                    properties.getProperty("user"),
                    properties.getProperty("password")
            );
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws SQLException {
        // MARTIN
        String sql = "select * from emp where ENAME like ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setString(1,"_A%");
        ResultSet resultSet = preparedStatement.executeQuery();

        while (resultSet.next()){
            String empno = resultSet.getString(1);
            String ename = resultSet.getString(2);
            System.out.println(empno+"\t"+ename);
        }

        preparedStatement.close();
        connection.close();
    }

}
